1 // Fig. 18.4: fig18_04.cpp 2 // Using the exit and atexit functions 3 #include 4 #include 5 6 void print( void ); 7 8 int main() 9 { 10 atexit( print ); // register function print 11 cout << "Enter 1 to terminate program with function exit" 12 << "\nEnter 2 to terminate program normally\n"; 13 14 int answer; 15 cin >> answer; 16 17 if ( answer == 1 ) { 18 cout << "\nTerminating program with function exit\n"; 19 exit( EXIT_SUCCESS ); 20 } 21 22 cout << "\nTerminating program by reaching the of main" 23 << endl; 24 25 return 0; 26 } 27 28 void print( void ) 29 { 30 cout << "Executing function print at program termination\n" 31 << "Program terminated" << endl; 32 }